<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyWord" placeholder="产品名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTypes">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">添加</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--产品信息显示列表-->
		<el-table :data="types" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="产品编号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="name" label="产品名称" width="120" sortable >
			</el-table-column>
			<el-table-column label="操作" width="250">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                    <el-button @click="getDetails(scope.$index, scope.row)"  type="primary" size="small" >明细</el-button>
				</template><!--@open="getDetails(scope.$index, scope.row)"-->
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!--添加、修改界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="产品编号" prop="sn">
					<el-input v-model="editForm.sn" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品名称" prop="name">
					<el-input v-model="editForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

        <!--明细表添加、修改界面-->
        <el-dialog :title="title" :visible.sync="editFormVisibleDetail" :close-on-click-modal="false">
            <el-form :model="editFormDetail" label-width="80px" :rules="editFormRulesDetail" ref="editFormDetail">
                <el-form-item label="明细" prop="name">
                    <el-input v-model="editFormDetail.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="数据字典" prop="type">
                    <!--<el-input v-model="editForm.manager" auto-complete="off"></el-input>-->
                    <!--elementUI中select选择器-->
                    <!--value-key="id"：作为 :value 唯一标识的键名，绑定值为对象类型时必填-->
                    <el-select v-model="editFormDetail.type" value-key="id" placeholder="请选择">
                        <!--:key：对象的唯一标识-->
                        <!--:label：选项的标签，若不设置则默认与 :value 相同-->
                        <!--:value：选项的值，这里应该是一个对象-->
                        <el-option
                                v-for="item in typesShow"
                                :key="item.id"
                                :label="item.name"
                                :value="item">
                            <span style="float: left">{{ item.name }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.sn }}</span>
                        </el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisibleDetail = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmitDetail" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--明细表-->
        <el-drawer title="数据字典明细" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
            <section>
                <!--明细表工具条-->
                <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
                    <el-form :inline="true" :model="filters">
                        <el-form-item>
                            <el-input v-model="filters.keyWord" placeholder="产品"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" v-on:click="gaoQuery">查询</el-button>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleAddDetail">添加</el-button>
                        </el-form-item>
                    </el-form>
                </el-col>

                <!--明细表信息显示列表-->
                <el-table :data="details" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
                    <el-table-column type="selection" width="55">
                    </el-table-column>
                    <el-table-column type="index" width="60">
                    </el-table-column>
                    <el-table-column prop="name" label="明细名" width="120" sortable>
                    </el-table-column>
                    <el-table-column label="操作" width="150">
                        <template scope="scope">
                            <el-button size="small" @click="handleEditDetail(scope.$index, scope.row)">修改</el-button>
                            <el-button type="danger" size="small" @click="handleDelDetail(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--明细表工具条-->
                <el-col :span="24" class="toolbar">
                    <el-button type="danger" @click="batchRemoveDetail" :disabled="this.sels.length===0">批量删除</el-button>
                    <el-pagination layout="prev, pager, next"
                                   @current-change="handleCurrentChangeDetail"
                                   :page-size="pageSize"
                                   :total="total"
                                   style="float:right;">
                    </el-pagination>
                </el-col>

            </section>
        </el-drawer>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getEmployeeListPage, removeEmployee, batchRemoveEmployee, editEmployee, addEmployee } from '../../api/api';

	export default {
		data() {
			return {
                drawer: false,
                direction: 'rtl',
                details: [],
                row: null,
                index: null,
                typesShow: [],
				filters: {
                    keyWord: ''
				},
				types: [],
				total: 0,
                currentPage: 1,
				pageSize: 5,
				listLoading: false,
				sels: [],//列表选中列
                title: "",
				editFormVisible: false,//修改界面是否显示
				editFormVisibleDetail: false,//修改界面是否显示
				editLoading: false,
				editFormRules: {
					sn: [
						{ required: true, message: '产品编号', trigger: 'blur' }
					],
					name: [
						{ required: true, message: '产品名称', trigger: 'blur' }
					]
				},
				editFormRulesDetail: {
					name: [
						{ required: true, message: '明细', trigger: 'blur' }
					]
				},
				//修改界面数据
				editForm: {
				    id: null,
                   	sn: '',
					name: '',
				},
				// 明细表修改界面数据
				editFormDetail: {
				    id: null,
					name: '',
                    type: null,
				},

				editFormVisible: false,//添加界面是否显示
				editFormVisibleDetail: false,//添加界面是否显示
				addLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '产品名称', trigger: 'blur' }
					]
				},
			}
		},
		methods: {
		    // 当前页发生改变时重新发送分页请求
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getTypes();
			},
			//获取产品列表
			getTypes() {
				let para = { // 分页+高级查询参数
					currentPage: this.currentPage,
					total: this.total,
					pageSize: this.pageSize,
                    keyWord: this.filters.keyWord // 高级查询参数
				};
				this.listLoading = true; // 忙等框开启
				/*发生post请求，分页请求，会被后端分页接口接收，并响应数据到result*/
				this.$http.post("/dataType", para)
					.then(result=>{
					    this.types = result.data.rows; // 填入分页数据
					    this.total = result.data.totals; // 填入数据总条数
                        this.listLoading = false; // 忙等框关闭
					}).catch(result=>{
					    console.debug(result)
						alert("系统繁忙")
				})
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => { // 发送请求成功时执行
					this.listLoading = true; // 忙等框
					// 携带参数发送删除请求，
					this.$http.delete("/dataType/" + row.id)
						.then(result=>{ // 请求发送成功时执行
                            this.listLoading = false; // 关闭忙等框
                            // 根据后端响应的对象进行判断
						    if (result.data.success) { // 删除成功时执行
                                this.$message({ // elementui中消息框
                                    message: result.data.msg, // 获取后端响应的删除成功信息
                                    type: 'success'
                                });
							} else {
                                this.$message({ // elementui中消息框
                                    message: result.data.msg, // 获取后端响应的删除成功信息
                                    type: 'error'
                                });
							}
                            this.currentPage = 1; // 给当前页赋值
						    this.getTypes(); // 加载数据，回到第一页
						}).catch(result=>{
                        this.$message({
                            message: "系统繁忙。。。。",
                            type: 'error'
                        });
					})
				}).catch(() => {

				});
			},
			//显示修改界面
			handleEdit: function (index, row) {
                this.title = "修改";
                this.editFormVisible = true; // 显示模态框
                this.editForm = Object.assign({}, this.editForm,row); // 回显数据
			},
			//显示添加界面
			handleAdd: function () {
			    this.title = "添加";
                this.editFormVisible = true; // 显示模态框
                this.editForm = {
                    sn: '',
					name: '',
				};
			},
			//修改
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true; // 按钮忙等框
                            //NProgress.start();
                            let para = Object.assign({}, this.editForm); // 克隆数据，将表单中的数据整合为一个对象
                            // // 处理传输数据。----添加数据时上级产品使用
                            // if (para.department) { // 当parent中有数据时执行
                            //     let p = para.department; // 获取parent中数据
                            //     let id = p[p.length - 1]; // 获取parent中最后一个数据
                            //     para.department = {"id" : id}; // 将数据封装成对象
                            // }
                            this.$http.put("/dataType", para)
                                .then(result=>{
                                    this.editFormVisible = false; // 成功与否都关闭模态框
                                    this.editLoading = false; // 成功与否都关闭按钮模态框
                                    if (result.data.success) {
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    } else {
                                        this.$message({
                                            message: result.data.msg,
                                            type: 'error'
                                        });
                                    }
                                    this.getTypes(); // 重新查询数据
                                }).catch(result=>{
                                this.$message({
                                    message: "系统繁忙111111111。、。。。",
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
			    // sels:数组，将选择的数据的id值取出封装为一个数组，并转换为String类型
                // var ids = this.sels.map(item => item.id).toString();

                var ids = this.sels.map(item => item.id) // sels:数组，将选择的数据的id值取出封装为一个数组
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true; // 开启忙等框
					this.$http.patch("/dataType", ids)
						.then(result=>{
                            this.listLoading = false; // 关闭忙等框
							if (result.data.success) {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'success'
                                });
							} else {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'error'
                                });
							}
                            this.currentPage = 1; // 给当前页赋值
                            this.getTypes(); // 加载数据，回到第一页
						}).catch(result=>{
                        this.$message({
                            message: "系统繁忙。。。",
                            type: 'error'
                        });
					})
				}).catch(() => {

				});
			},

            //-------------------------------------------------------------------------------------------------------
            // 明细表显示添加界面
            handleAddDetail: function () {
                this.title = "添加";
                this.editFormVisibleDetail = true; // 显示模态框
                this.editFormDetail = {
                    name: '',
                    type:null,
                };
            },

            // 明细表显示修改界面
            handleEditDetail: function (index, row) {
                this.title = "修改";
                this.editFormVisibleDetail = true; // 显示模态框
                this.editFormDetail = Object.assign({}, this.editFormDetail,row); // 回显数据
            },

            // 明细表修改
            editSubmitDetail: function () {
                this.$refs.editFormDetail.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.editLoading = true; // 按钮忙等框
                            //NProgress.start();
                            let para = Object.assign({}, this.editFormDetail); // 克隆数据，将表单中的数据整合为一个对象
                            this.$http.put("/dataDetail", para)
                                .then(result=>{
                                    this.editFormVisibleDetail = false; // 成功与否都关闭模态框
                                    this.editLoading = false; // 成功与否都关闭按钮模态框
                                    if (result.data.success) {
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                    } else {
                                        this.$message({
                                            message: result.data.msg,
                                            type: 'error'
                                        });
                                    }
                                    this.getDetails(this.index, this.row); // 重新查询数据
                                }).catch(result=>{
                                this.$message({
                                    message: "系统繁忙111111111。、。。。",
                                    type: 'error'
                                });
                            })
                        });
                    }
                });
            },

            // 明细表删除
            handleDelDetail: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => { // 发送请求成功时执行
                    this.listLoading = true; // 忙等框
                    // 携带参数发送删除请求，
                    this.$http.delete("/dataDetail/" + row.id)
                        .then(result=>{ // 请求发送成功时执行
                            this.listLoading = false; // 关闭忙等框
                            // 根据后端响应的对象进行判断
                            if (result.data.success) { // 删除成功时执行
                                this.$message({ // elementui中消息框
                                    message: result.data.msg, // 获取后端响应的删除成功信息
                                    type: 'success'
                                });
                            } else {
                                this.$message({ // elementui中消息框
                                    message: result.data.msg, // 获取后端响应的删除成功信息
                                    type: 'error'
                                });
                            }

                            this.currentPageDetail = 1; // 给当前页赋值
                            this.getDetails(this.index, this.row) // 加载数据，回到第一页
                        }).catch(result=>{
                        this.$message({
                            message: "系统繁忙。。。。",
                            type: 'error'
                        });
                    })
                }).catch(() => {

                });
            },

            // 明细表批量删除
            batchRemoveDetail: function () {
                // sels:数组，将选择的数据的id值取出封装为一个数组，并转换为String类型
                // var ids = this.sels.map(item => item.id).toString();

                var ids = this.sels.map(item => item.id) // sels:数组，将选择的数据的id值取出封装为一个数组
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true; // 开启忙等框
                    this.$http.patch("/dataDetail", ids)
                        .then(result=>{
                            this.listLoading = false; // 关闭忙等框
                            if (result.data.success) {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'success'
                                });
                            } else {
                                this.$message({
                                    message: result.data.msg,
                                    type: 'error'
                                });
                            }
                            this.currentPage = 1; // 给当前页赋值
                            this.getDetails(this.index, this.row); // 加载数据，回到第一页
                        }).catch(result=>{
                        this.$message({
                            message: "系统繁忙22。。。",
                            type: 'error'
                        });
                    })
                }).catch(() => {

                });
            },
            /*展示/关闭抽屉*/
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },

            // 分页页面变化时查询
            handleCurrentChangeDetail(val) {
                this.currentPage = val;
                this.getDetails(this.index, this.row);
            },

            // 高级查询
            gaoQuery(){
                this.getDetails(this.index, this.row);
            },

            // 分页查询加高级查询
            getDetails: function (index, row) {
                /* 将当前操作的数据字典的属性存入公共属性中 */
                this.index = index;
                this.row = row;

                this.drawer = true; // 打开抽屉
                let para = { // 分页+高级查询参数
                    currentPage: this.currentPage,
                    id: row.id,
                    total: this.total,
                    pageSize: this.pageSize,
                    keyWord: this.filters.keyWord // 高级查询参数
                };
                this.listLoading = true; // 忙等框开启
                /*发生post请求，分页请求，会被后端分页接口接收，并响应数据到result*/
                this.$http.post("/dataDetail", para)
                    .then(result=>{
                        this.details = result.data.rows; // 填入分页数据
                        this.total = result.data.totals; // 填入数据总条数
                        this.listLoading = false; // 忙等框关闭
                    }).catch(result=>{
                    console.debug(result)
                    alert("系统繁忙11")
                })
            },

            getTypesToShow: function () {
                this.$http.get("/dataType")
                    .then(result=>{
                        this.typesShow = result.data
                    }).catch(result=>{
                        alert("获取数据字典失败")
                })
            }

		},
		mounted() {
			this.getTypes();
			this.getTypesToShow()
		}
	}

</script>

<style scoped>

</style>